const { query } = require("./mysql");
const {
	YD_datetimeToString,
	YD_whereSql,
	YD_setSql,
} = require("lib/utils/common");

/** @name 添加会员副表 **/
exports.yd_addVice = (options) => {
	const sql = `insert into ch_vich_vice set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 查询会员副表-number **/
exports.yd_findVice = (options) => {
	const sql = `select
						*,
						${YD_datetimeToString("createTime")},
						${YD_datetimeToString("updateTime")}
						from ch_vich_vice
						${YD_whereSql([
							{ field: "number", type: "全等", value: options.number },
							{ field: "packageId", type: "全等", value: options.packageId },
						])}
						`;
	return query(sql);
};

/** @name 查询会员副表-id **/
exports.yd_queryVice = (options) => {
	const sql = `select
						pvv.id, pvv.number, pvv.orderCode, pvv.status, pvv.payType,
						yp.code, yp.name, yp.value, yp.price, yp.note,
						${YD_datetimeToString("pvv.createTime")},
						${YD_datetimeToString("pvv.updateTime")}
						from ch_vich_vice pvv
						left join ch_package yp on pvv.packageId=yp.id
						${YD_whereSql([{ field: "pvv.id", type: "全等", value: options.id }])}
						`;
	return query(sql);
};

/** @name 编辑会员副表 **/
exports.yd_updateVice = (options, condition) => {
	const sql = `update ch_vich_vice set ${YD_setSql(options)} where number='${
		condition.number
	}' and packageId=${condition.packageId}`;
	return query(sql);
};

/** @name 查询会员-number **/
exports.yd_find = (options) => {
	const sql = `select
						*,
						${YD_datetimeToString("startTime")},
						${YD_datetimeToString("endTime")}
						from ch_vip
						${YD_whereSql([{ field: "number", type: "全等", value: options.number }])}
						`;
	return query(sql);
};

/** @name 添加会员 **/
exports.yd_add = (options) => {
	const sql = `insert into ch_vip set ${YD_setSql(options)}`;
	return query(sql);
};

/** @name 编辑会员 **/
exports.yd_update = (options, condition) => {
	const sql = `update ch_vip set ${YD_setSql(options)} where id=${
		condition.id
	}`;
	return query(sql);
};

/** @name 查询会员-userId **/
exports.yd_findVip = (options) => {
	const sql = `select
						*,
						${YD_datetimeToString("startTime")},
						${YD_datetimeToString("endTime")}
						from ch_vip
						${YD_whereSql([{ field: "userId", type: "全等", value: options.userId }])}
						`;
	return query(sql);
};
